import { IBreadCrumb } from '@/base-ui/breadcrumb'
import menu from '@/router/main/system/menu/menu'
import { RouteRecordRaw } from 'vue-router'
let firstMenu: any = null
export function mapMenusToRoutes(userMenus: any[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = []
  //1.先去加载默认所有的routes
  const allRoutes: RouteRecordRaw[] = []
  //这个是webpack自带的方法  用来加载一个文件夹   第一个参数为哪个文件夹  第二个为是否递归查找  第三个参数是一个正则用来匹配所有ts文件
  const routeFiles = require.context('../router/main', true, /\.ts$/)
  routeFiles.keys().forEach((key) => {
    const route = require('../router/main' + key.split('.')[1])
    allRoutes.push(route.default)
  })

  // 2.根据菜单获取需要添加的routes
  const _recurseGetRoute = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 2) {
        const route = allRoutes.find((route) => {
          return route.path.includes(menu.url)
        })
        if (route) {
          routes.push(route)
        }
        if (!firstMenu) {
          firstMenu = menu
        }
      } else {
        _recurseGetRoute(menu.children)
      }
    }
  }
  _recurseGetRoute(userMenus)
  return routes
}

// export function pathMapBreadcrumbs(userMenus:any[],currentPath:string):object[]{
//   const breadcrumbs:IBreadCrumb[] = []
//   for(const menu of userMenus){
//     //如果type=1 说明还有子菜单  需要继续遍历
//     if(menu.type==1){
//       const findMenu = pathMap(menu.children??[],currentPath)
//       if(findMenu){
//         breadcrumbs.push({name:menu.name,path:menu.url})
//         breadcrumbs.push({name:findMenu.name,path:findMenu.url})
//         return findMenu
//       }
//     }else if(menu.type==2&&menu.url==currentPath){
//       return menu
//     }
//   }
//   return breadcrumbs
// }


// export function pathMap(userMenus:any[],currentPath:string):any{
//   for(const menu of userMenus){
//     //如果type=1 说明还有子菜单  需要继续遍历
//     if(menu.type==1){
//       const findMenu = pathMap(menu.children??[],currentPath)
//       if(findMenu){
//         return findMenu
//       }
//     }else if(menu.type==2&&menu.url==currentPath){
//       return menu
//     }
//   }
// }


export function pathMapBreadcrumbs(
  userMenus: any[],
  currentPath: string
): IBreadCrumb[] {
  const breadcrumbs: IBreadCrumb[] = []
  pathMap(userMenus,currentPath,breadcrumbs)

  return breadcrumbs
}


export function pathMap(
  userMenus: any[],
  currentPath: string,
  breadcrumbs?: IBreadCrumb[]
): any {
  for (const menu of userMenus) {
    //如果type=1 说明还有子菜单  需要继续遍历
    if (menu.type == 1) {
      const findMenu = pathMap(menu.children ?? [], currentPath)
      if (findMenu) {
        breadcrumbs?.push({ name: menu.name})
        breadcrumbs?.push({ name: findMenu.name})
        return findMenu
      }
    } else if (menu.type == 2 && menu.url == currentPath) {
      return menu
    }
  }
}


export function mapMenusToPermissions(userMenus:any[]){
  const permission:string[]=[]
  const _recurseGetPermission = (menus:any[])=>{
    for(const menu of menus){
      if(menu.type===1||menu.type===2){
        _recurseGetPermission(menu.children??[])
      }else if(menu.type===3){
        permission.push(menu.permission)
      }
    }
  }
  _recurseGetPermission(userMenus)
  return permission
}

export function getMenuLeafKeys(menuList:any[]){
  const leafKeys:number[] = []
  const _recurseGetLeaf = (menuList:any[])=>{
    for(const menu of menuList){
      if(menu.children){
        _recurseGetLeaf(menu.children)
      }else{
        leafKeys.push(menu.id)
      }
    }
  }
  _recurseGetLeaf(menuList)
  return leafKeys
}
export { firstMenu }
